<?php
/**
 * Created by PhpStorm.
 * User: a4041
 * Date: 2018/6/13
 * Time: 13:46
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ShopRole extends Model
{
    protected $guarded = ['id'];

    /*
     * 当前角色的所有权限
     */
    public function permissions()
    {
        return $this->belongsToMany(\App\Models\ShopPermission::class, 'shop_permission_role', 'permission_id', 'role_id')->withPivot(['permission_id', 'role_id']);
    }

    /*
     * 给角色授权
     */
    public function grantPermission($permission)
    {
        return $this->permissions()->save($permission);
    }

    /*
     * 删除role和permission的关联
     */
    public function deletePermission($permission)
    {
        return $this->permissions()->detach($permission);
    }

    /*
     * 角色是否有权限
     */
    public function hasPermission($permission)
    {
        return $this->permissions->contains($permission);
    }
}